package com.class5.springboot.frame.repo.tool.xml;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("serial")
public class DbProject implements Serializable {

    // 默认的数据库
    private String[] dbs;
    // 项目名称
    private String project;
    // 表名的前缀，主要用于自动删除表，未给定时将不会自动删除表
    private String prefix;
    //说明
    private String notes;
    // 主数据库，存放执行后的xml数据
    private String masterdb;
    // 实体包
    private String entityPackage;
    // 逻辑表声明
    private List<DbTable> tables = new ArrayList<DbTable>();
    //字典组
    private Map<String, DbCodeGroup> codeGroup = new HashMap<>();
    //初始化数据
    private List<DbInit> initdatas = new ArrayList<>();
    //缓存
    private Map<String, DbTable> tableMap = null;
    private Map<String, DbTable> entityMap = null;


    public Map<String, DbCodeGroup> getCodeGroup() {
        return codeGroup;
    }

    public void setCodeGroup(Map<String, DbCodeGroup> codeGroup) {
        this.codeGroup = codeGroup;
    }

    public String[] getDbs() {
        return dbs;
    }

    public void setDbs(String[] dbs) {
        this.dbs = dbs;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public List<DbTable> getTables() {
        return tables;
    }

    public void setTables(List<DbTable> tables) {
        this.tables = tables;
        Collections.unmodifiableCollection(this.tables);
        tableMap = new HashMap<String, DbTable>();
        entityMap = new HashMap<String, DbTable>();
        for(DbTable table : tables){
            tableMap.put(table.getName(), table);
            entityMap.put(table.getEntity(), table);
        }
        Collections.unmodifiableMap(tableMap);
        Collections.unmodifiableMap(entityMap);
    }

    public String getMasterdb() {
        return masterdb;
    }

    public void setMasterdb(String masterdb) {
        this.masterdb = masterdb;
    }

    public String getEntityPackage() {
        return entityPackage;
    }

    public void setEntityPackage(String entityPackage) {
        this.entityPackage = entityPackage;
    }

    @Override
    public String toString() {
        return "DbProject [dbs=" + Arrays.toString(dbs) + ", project="
                + project + ", masterdb=" + masterdb + ", entityPackage="
                + entityPackage + ", tables=" + tables + ", tableMap="
                + tableMap + "]";
    }

    public boolean hasTable(String name) {
        for(DbTable t : tables){
            if(t.getName().equals(name)){
                return true;
            }
        }
        return false;
    }

    public DbTable getTable(String name) {
        return tableMap.get(name);
    }

    public DbTable getTableByEntity(String className) {
        return entityMap.get(className);
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public List<DbInit> getInitdatas() {
        return initdatas;
    }

    public void setInitdatas(List<DbInit> initdatas) {
        this.initdatas = initdatas;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }


}